package com.sghd.fight.module.game.core;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.sghd.pojo.fight.game.ChessType;
import com.sghd.pojo.fight.game.ChessVo;

/**
 * @author frank
 * @date 2018-10-18 16:49
 * @desc
 **/
public class Chess {

    private int state;//00000

    @JsonFormat(shape = JsonFormat.Shape.NUMBER)
    private ChessType type;

    public Chess() {
    }

    public Chess(ChessType type) {
        this.type = type;
    }

    public Chess(int state, ChessType type) {
        this.state = state;
        this.type = type;
    }

    public ChessType getType() {
        return type;
    }

    public void setType(ChessType type) {
        this.type = type;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    @JsonIgnore
    public boolean isEmpty() {
        return type == null;
    }

    @JsonIgnore
    public void remove() {
        if (type == null) {
            return;
        }
        type = null;
    }

    @JsonIgnore
    public void addStatus(int state) {
        if (hasStatus(state)) {
            return;
        }
        this.state |= state;
    }

    @JsonIgnore
    public boolean hasStatus(int state) {
        return (this.state & state) == state;
    }

    @JsonIgnore
    public void removeStatus(int state) {
        if (hasStatus(state)) {
            this.state ^= state;
        }
    }

    public Chess copy() {
        Chess chess = new Chess(this.type);
        chess.state = this.state;
        return chess;
    }

    @Override
    public String toString() {
        return type.toString();
    }

    public ChessVo toVo() {
        return ChessVo.valueOf(state, type);
    }
}
